package cn.iocoder.yudao.module.shop.service.item;

import cn.hutool.core.img.ColorUtil;
import cn.iocoder.yudao.module.shop.convert.itemconsume.ShopItemConsumeConvert;
import cn.iocoder.yudao.module.shop.dal.dataobject.itemconsume.ShopItemConsumeDO;
import cn.iocoder.yudao.module.shop.dal.mysql.itemconsume.ShopItemConsumeMapper;
import cn.iocoder.yudao.module.shop.dal.mysql.store.StoreMapper;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.shop.controller.admin.item.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.item.ShopItemDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.shop.convert.item.ShopItemConvert;
import cn.iocoder.yudao.module.shop.dal.mysql.item.ShopItemMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 店铺项目 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class ShopItemServiceImpl implements ShopItemService {

    @Resource
    private ShopItemMapper itemMapper;

    @Resource
    private StoreMapper storeMapper;

    @Resource
    private ShopItemConsumeMapper shopItemConsumeMapper;

    @Override
    public Long createItem(ShopItemCreateReqVO createReqVO) {
        if(createReqVO.getStoreIdList().size() != createReqVO.getCountList().size()){
            throw exception(ITEM_CREATE_CONSUME_ERROR);
        }

        for(Long storeId: createReqVO.getStoreIdList()){
            if(storeMapper.selectById(storeId) == null){
                throw exception(STORE_NOT_EXISTS);
            }
        }

        // 插入
        // 客户端发来单位是 元
        createReqVO.setPrice(createReqVO.getPrice() * 100);
        createReqVO.setHandlePrice(createReqVO.getHandlePrice()* 100);
        ShopItemDO item = ShopItemConvert.INSTANCE.convert(createReqVO);
        itemMapper.insert(item);

        Long itemId = item.getId();

        // 创建消耗列表
        for(int i = 0; i<createReqVO.getStoreIdList().size(); i++){
            Long storeId = createReqVO.getStoreIdList().get(i);
            Integer count = createReqVO.getCountList().get(i);

            ShopItemConsumeDO consumeDO = ShopItemConsumeConvert.INSTANCE.convert(itemId, storeId, count);
            shopItemConsumeMapper.insert(consumeDO);
        }

        // 返回
        return itemId;
    }

    @Override
    public void updateItem(ShopItemUpdateReqVO updateReqVO) {
        // 校验存在
        validateItemExists(updateReqVO.getId());
        // 更新
        updateReqVO.setPrice(updateReqVO.getPrice() * 100);
        updateReqVO.setHandlePrice(updateReqVO.getHandlePrice()* 100);
        ShopItemDO updateObj = ShopItemConvert.INSTANCE.convert(updateReqVO);
        itemMapper.updateById(updateObj);

        // 删除消耗列表
        if(updateReqVO.getDelConsumeIdList() != null){
            if(updateReqVO.getDelConsumeIdList().size() > 0)
            {
                shopItemConsumeMapper.deleteBatchIds(updateReqVO.getDelConsumeIdList());
            }
        }

        // 新增的消耗列表
        if(updateReqVO.getStoreIdList() != null && updateReqVO.getCountList() != null){
            if(updateReqVO.getStoreIdList().size() != updateReqVO.getCountList().size()){
                throw exception(ITEM_CREATE_CONSUME_ERROR);
            }

            for(int i = 0; i<updateReqVO.getStoreIdList().size(); i++){
                Long storeId = updateReqVO.getStoreIdList().get(i);
                Integer count = updateReqVO.getCountList().get(i);

                ShopItemConsumeDO consumeDO = ShopItemConsumeConvert.INSTANCE.convert(updateReqVO.getId(), storeId, count);
                shopItemConsumeMapper.insert(consumeDO);
            }
        }
    }

    @Override
    public void deleteItem(Long id) {
        // 校验存在
        validateItemExists(id);
        // 删除
        itemMapper.deleteById(id);
    }

    private void validateItemExists(Long id) {
        if (itemMapper.selectById(id) == null) {
            throw exception(ITEM_NOT_EXISTS);
        }
    }

    @Override
    public ShopItemDO getItem(Long id) {
        return itemMapper.selectById(id);
    }

    @Override
    public List<ShopItemDO> getItemList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return itemMapper.selectBatchIds(ids);
    }

    @Override
    public List<ShopItemDO> getItemByTenantIds(Collection<Long> tenantIds){
        if(CollUtil.isEmpty(tenantIds)){
            return ListUtil.empty();
        }

        return itemMapper.selectList(ShopItemDO::getTenantId, tenantIds);
    }

    @Override
    public Map<Long, ShopItemRespVO> getItemMap(Collection<Long> ids) {
        List<ShopItemDO> list = getItemList(ids);
        return ShopItemConvert.INSTANCE.convertShopItemMap(list);
    }

    @Override
    public PageResult<ShopItemDO> getItemPage(ShopItemPageReqVO pageReqVO) {
        return itemMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShopItemDO> getItemList(ShopItemExportReqVO exportReqVO) {
        return itemMapper.selectList(exportReqVO);
    }

}
